package com.huawei.M01.iterator.M09;

import java.util.HashMap;
import java.util.Map;

/**
 * 2379. 得到 K 个黑块的最少涂色次数
 *
 * @author 111
 */
public class MinimumRecolors {
    public int minimumRecolors(String blocks, int k) {

        Map<Character, Integer> cache = new HashMap<>(2);
        for (int i = 0; i < k; i++) {
            cache.put(blocks.charAt(i), cache.getOrDefault(blocks.charAt(i), 0) + 1);
        }
        int res = cache.getOrDefault('W', 0);
        int left = 0;
        int right = k - 1;
        while (right < blocks.length() - 1) {
            cache.put(blocks.charAt(left), cache.get(blocks.charAt(left)) - 1);
            cache.put(blocks.charAt(right + 1), cache.getOrDefault(blocks.charAt(right + 1), 0) + 1);
            res = Math.min(cache.getOrDefault('W', 0), res);
            if (res == 0) {
                return res;
            }

            left++;
            right++;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new MinimumRecolors().minimumRecolors("WWBBBWBBBBBWWBWWWB", 16));
    }
}
